﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xww.Task;
using System.Text.RegularExpressions;

namespace Xww.LCV.Generator {
    public static class GeneratorShare {
        /// <summary>
        /// 产生初始化 LCV 种子，单元个数为2
        /// </summary>
        /// <param name="lines">MasterSet</param>
        /// <param name="MaxIntervalAtFirst">允许的最大间隔（不包括两端）</param>
        /// <param name="Threshold">阈值</param>
        public static HashSet<LCS> FirstStep(this BaseGenerator generator, IEnumerable<string> lines, int MaxIntervalAtFirst, int Threshold) {
            HashSet<LCS> noLCSs = new HashSet<LCS>(LCS.InstanceForHash);
            HashSet<LCS> rst = new HashSet<LCS>(LCS.InstanceForHash);
            ProgressStatus status = new ProgressStatus(lines.Count(), "First step : generating LCS of 2 units");
            foreach (string sequence in lines) {
                generator.StepChange(status);
                for (int first = 0; first < sequence.Length - 1; first++) {
                    LCSUnit FirstUnit = new LCSUnit(sequence[first], 1);
                    for (int second = first + 1; second < sequence.Length; second++) {
                        int offset = second - first -1;
                        if (offset > MaxIntervalAtFirst) break;
                        LCS seed = new LCS(new char[] { FirstUnit.AA, sequence[second] }, new int[] { 1, offset + 2 });
                        if (noLCSs.Contains(seed) || rst.Contains(seed))
                            continue;
                        seed.Support = lines.Count(o => Regex.IsMatch(o, seed.RegexString));
                        seed.DeleteRegexString();
                        if (seed.Support >= Threshold)
                            rst.Add(seed);
                        else
                            noLCSs.Add(seed);
                    }
                }
                status.ProgressOneStep();
            }
            noLCSs = null;
            return rst;
        }
    }
}
